package app.android.sotaynauan;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import app.android.database.Constants;
import app.android.database.MyDatabaseAdapter;
import app.android.model.Food;

public class HistoryListActivity extends ListActivity implements OnItemClickListener {
	private MyDatabaseAdapter mAdapter = null;
	private ListView lv = null;	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mAdapter = new MyDatabaseAdapter(this);
		lv = getListView();
				
		String[] dataSource = getHistoryDataSource();
		
		setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, dataSource)) ;
		
		lv.setOnItemClickListener(this);
		
		ActionBar actionBar = getActionBar();
//	    actionBar.setDisplayShowTitleEnabled(false);
	    actionBar.setDisplayShowHomeEnabled(false);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_history_list, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.menu_list_all:     
			Intent launchListAll = new Intent(this, MainActivity.class);			
			startActivity(launchListAll);	
			finish();
			break;	
		case R.id.menu_clear:   
			mAdapter.deleteAllHistory();
			lv.setAdapter(null);
			finish();
//			Intent launchListAll = new Intent(this, MainActivity.class);			
//			startActivity(launchListAll);			
			break;			
		}
	
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
		// TODO Auto-generated method stub
		Bundle bundle = new Bundle();
		bundle.putInt(MainActivity.DETAIL_ID,(int)id);		
		FoodDetailActivity.startFoodDetail(HistoryListActivity.this, view, bundle);
	}
	
	private String[]  getHistoryDataSource() {  
		mAdapter.deleteOldHistory();
        Cursor cursor = mAdapter.getAllHistories();
           
        int foodIdx = cursor.getColumnIndexOrThrow(Constants.HIS_FOOD_ID);
        //int timeIdx = cursor.getColumnIndexOrThrow(Constants.HIS_TIME);

        String[] result = new String[cursor.getCount()];
      
        while(cursor.moveToNext()) {
          int foodId = cursor.getInt(foodIdx);	        
          //String time = cursor.getString(timeIdx);
          
          Food f = mAdapter.getFoodByID(foodId);
          
          if (f != null)
        	  result[cursor.getPosition()] = f.getFoodName();
        } 
      
        cursor.close();

        return result;
    }

}
